package com.qqtech.qquan.share.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.enums.MallShopStyleDictEnum;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.order.enums.OrderBaseOrderTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayStatusDictEnum;
import com.qqtech.qquan.order.model.OrderBaseVo;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.share.cache.ShareSysParamCache;
import com.qqtech.qquan.share.dao.ShareBaseDao;
import com.qqtech.qquan.share.enums.ShareBaseBizzTypeDictEnum;
import com.qqtech.qquan.share.enums.ShareConst;
import com.qqtech.qquan.share.model.ShareBase;
import com.qqtech.qquan.share.model.ShareBaseQuery;
import com.qqtech.qquan.user.enums.UserMoneyLogTypeDictEnum;
import com.qqtech.qquan.user.service.UserMoneyLogService;

@Service
public class ShareBaseServiceImpl extends BaseServiceImpl<ShareBase> implements ShareBaseService {
	public static final Logger log = LoggerFactory.getLogger(ShareBaseServiceImpl.class);

	@Resource
	private ShareSysParamCache shareSysParamCache;

	@Resource
	private ShareBaseDao shareBaseDao;

	@Override
	protected BaseDao<ShareBase> getBaseDao() {
		return shareBaseDao;
	}

	@Resource
	private MallShopService mallShopService;

	@Resource
	private UserMoneyLogService userMoneyLogService;

	@Resource
	private OrderBaseService orderBaseService;

	@Override
	public Result showShare(Integer bizzId, Integer bizzType, Integer userId) {
		Result ret = new Result();
		boolean canShare = false;
		canShare = canShare(bizzId, bizzType, userId);
		ret.put("canShare", canShare);
		return ret;
	}

	private boolean canShare(Integer bizzId, Integer bizzType, Integer userId) {
		boolean canShare = false;
		if (ShareBaseBizzTypeDictEnum.ORDER.getCode() == bizzType) {// 订单分享
			OrderBaseVo orderBaseVo = orderBaseService.getById(bizzId);
			if (OrderBasePayStatusDictEnum.PAYED.getCode() != orderBaseVo.getPayStatus()) {// 非支付完成订单
				return canShare;
			}
			if (OrderBaseOrderTypeDictEnum.QSHOP.getCode() != orderBaseVo.getOrderType()) {// 非圈商订单，邻里店，周边商家
				return canShare;
			} else {
				MallShop mallShop = mallShopService.getById(orderBaseVo.getShopId());
				if (MallShopStyleDictEnum.LOCAL.getCode() != mallShop.getStyle()
						&& MallShopStyleDictEnum.QQSTORE.getCode() != mallShop.getStyle()) {// 非周边商家订单&&非圈圈便利店
					return canShare;
				}
			}
			// 分享订单参数
			String shareOrderAwardStartTime = shareSysParamCache.get(ShareConst.PARAMKEY_SHARE_ORDER_AWARD_START_TIME);
			String shareOrderAwardEndTime = shareSysParamCache.get(ShareConst.PARAMKEY_SHARE_ORDER_AWARD_END_TIME);
			if (StringUtil.isNotBlank(shareOrderAwardStartTime) && StringUtil.isNotBlank(shareOrderAwardEndTime)) {
				if (TimeUtil.now().before(TimeUtil.getTimeByFormat(shareOrderAwardEndTime, TimeUtil.DATE_TIME_FORMAT))
						&& TimeUtil.now()
								.after(TimeUtil.getTimeByFormat(shareOrderAwardStartTime, TimeUtil.DATE_TIME_FORMAT))) {
					ShareBaseQuery query = new ShareBaseQuery();
					query.setUserId(userId);
					query.setBizzType(bizzType);
					query.setOnlyToday(YesNoEnum.YES.getCode());
					int count = shareBaseDao.queryCount(query);
					if (count <= 0) {// 今天没分享了
						query.setOnlyToday(null);
						query.setBizzId(bizzId);
						count = shareBaseDao.queryCount(query);
						if (count <= 0) {// 当前订单也没分享
							canShare = true;
						}
					}

				}
			}
		}
		return canShare;
	}

	@Override
	public Integer addShare(ShareBase shareBase) {
		Integer id = null;
		if (ShareBaseBizzTypeDictEnum.ORDER.getCode() == shareBase.getBizzType()) {// 订单分享
			boolean canShare = canShare(shareBase.getBizzId(), shareBase.getBizzType(), shareBase.getUserId());
			if (canShare) {
				String shareOrderAwardValue = shareSysParamCache.get(ShareConst.PARAMKEY_SHARE_ORDER_AWARD_VALUE);
				userMoneyLogService.addMoneyToUser(shareBase.getUserId(), Integer.valueOf(shareOrderAwardValue),
						"分享订单奖励" + shareOrderAwardValue + "圈币", UserMoneyLogTypeDictEnum.QUAN);
				id = shareBaseDao.insert(shareBase);
			}
		} else if (ShareBaseBizzTypeDictEnum.DRAW.getCode() == shareBase.getBizzType()) {
				id = shareBaseDao.insert(shareBase);
		}

		return id;
	}

}